package account;

import java.io.IOException;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;


public class AccountCreate extends HttpServlet
{
	
	 
	private static final long serialVersionUID = 1L;

	public void doPost(HttpServletRequest request, HttpServletResponse response)
	throws ServletException, IOException 
	{
		this.doGet(request, response);
	}
	
	public void doGet(HttpServletRequest request, HttpServletResponse response)
	throws ServletException, IOException 
	{
		String usuario = String.valueOf(request.getParameter("usuario"));
		String pass = String.valueOf(request.getParameter("password"));
		String tipo = String.valueOf(request.getParameter("tipo"));
		String nome = String.valueOf(request.getParameter("nome"));
		String rg = String.valueOf(request.getParameter("rg"));
		String cpf = String.valueOf(request.getParameter("cpf"));
		String endereco = String.valueOf(request.getParameter("endereco"));
		String telefone = String.valueOf(request.getParameter("telefone"));
		String email = String.valueOf(request.getParameter("email"));
		String dia = String.valueOf(request.getParameter("dia"));
		String mes = String.valueOf(request.getParameter("mes"));
		String ano = String.valueOf(request.getParameter("ano"));
		String sexo = String.valueOf(request.getParameter("sexo"));
		
		boolean createSuccess = AccountDB.createAccount(usuario, pass, 
				tipo, nome, rg, cpf, endereco, telefone, email, 
				Integer.parseInt(dia), Integer.parseInt(mes), Integer.parseInt(ano),
				sexo.charAt(0)); 
		
		if(createSuccess)
		{
			request.getSession().setAttribute("password", pass);
			request.getSession().setAttribute("userName", usuario);
			request.getSession().setAttribute("loginState", tipo);
			request.setAttribute("cadastroStatus", "Sucesso");
			
			RequestDispatcher rdIndex = request.getRequestDispatcher("index.jsp");
			rdIndex.forward(request, response);
		}
		else
		{
			
			request.setAttribute("cadastroStatus", "Erro");
			RequestDispatcher rdIndex = request.getRequestDispatcher("index.jsp");
			rdIndex.forward(request, response);
		}
			
		
				
	}
}
	
	
 